package com.nova.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.nova.BaseActivity;
import com.nova.source.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 日期选择框
 */
public class DatePickerDialog implements OnDateChangedListener {
	private DatePicker datePicker;
	private AlertDialog dialog;
	private String date;

	public DatePickerDialog(final BaseActivity activity, final String indate,
			final TextView tv) {
		super();

		this.date = indate;
		View view = activity.getLayoutInflater().inflate(
				R.layout.dialog_datepicker, null);
		datePicker = (DatePicker) view.findViewById(R.id.datepicker);
		init(datePicker);

		dialog = new AlertDialog.Builder(activity).setIcon(null)
				.setTitle("选择日期").setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
//						tv.setTextColor(activity.getResources().getColor(
//								R.color.deep_green));
						tv.setText(date);
					}
				}).setNegativeButton("取消", null).create();
		// dialog.setCanceledOnTouchOutside(true);
	}

	
	public DatePickerDialog(final FragmentActivity activity, final String indate,
			final EditText et) {
		super();

		this.date = indate;
		View view = activity.getLayoutInflater().inflate(
				R.layout.dialog_datepicker, null);
		datePicker = (DatePicker) view.findViewById(R.id.datepicker);
		init(datePicker);

		dialog = new AlertDialog.Builder(activity).setIcon(null)
				.setTitle("选择日期").setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						et.setTextColor(activity.getResources().getColor(
								R.color.deep_green));
						et.setText(date);
					}
				}).setNegativeButton("取消", null).create();
		// dialog.setCanceledOnTouchOutside(true);
	}
	
	public DatePickerDialog(final BaseActivity activity, final TextView tv,
			final StringBuffer sb) {
		super();

		View view = activity.getLayoutInflater().inflate(
				R.layout.dialog_datepicker, null);
		datePicker = (DatePicker) view.findViewById(R.id.datepicker);
		init(datePicker);

		dialog = new AlertDialog.Builder(activity).setIcon(null)
				.setTitle("选择日期").setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						tv.setTextColor(activity.getResources().getColor(
								R.color.deep_green));
						sb.append(date);
						tv.setText(sb.toString());
					}
				}).setNegativeButton("取消", null).create();
		// dialog.setCanceledOnTouchOutside(true);
	}

	private void init(DatePicker datePicker) {

		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		if (!date.equals("")) {
			try {
				Date setDate = sdf.parse(date);
				calendar.setTime(setDate);// //为Calendar对象设置时间为当前日期
				datePicker.init(calendar.get(Calendar.YEAR),
						calendar.get(Calendar.MONTH),
						calendar.get(Calendar.DAY_OF_MONTH),
						DatePickerDialog.this);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			date = sdf.format(calendar.getTime());
			datePicker.init(calendar.get(Calendar.YEAR),
					calendar.get(Calendar.MONTH),
					calendar.get(Calendar.DAY_OF_MONTH), DatePickerDialog.this);
		}

	}

	public void showDatePicker() {
		dialog.show();
	}

	@Override
	public void onDateChanged(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, monthOfYear, dayOfMonth);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd,HH:mm");
		String dd = sdf.format(calendar.getTime());
		String[] sss = dd.split(",");
		date = sss[0];
	}

}
